package com.spring.study.bean.definition;

import com.spring.study.domain.User;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * api的Setter
 */
public class ApiDependencySetterInjectionDemo {


    public static void main(String[] args) {
        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
       //生成UserHolder的BeanDefinition
        BeanDefinition beanDefinition = createUserHolderBeanDefinition();
        //注册UserHolder的BeanDefinition
        applicationContext.registerBeanDefinition("userHolder",beanDefinition);

        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);

        String xmlPath = "classpath:/META-INF/dependency-lookup-context.xml";
        //加载xml资源，解析并生成beanDefinition
        beanDefinitionReader.loadBeanDefinitions(xmlPath);

        // 启动 Spring 应用上下文
        applicationContext.refresh();

        //依赖查找并创建bean
        UserHolder userHolder = applicationContext.getBean(UserHolder.class);
        System.out.println(userHolder);

        // 显示地关闭 Spring 应用上下文
        applicationContext.close();
    }

    private static BeanDefinition createUserHolderBeanDefinition() {
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(UserHolder.class);
        beanDefinitionBuilder.addPropertyReference("user","superUser");
        return beanDefinitionBuilder.getBeanDefinition();
    }


//    @Bean
//    public UserHolder userHolder (User user) {
//        UserHolder userHolder = new UserHolder();
//        userHolder.setUser(user);
//        return userHolder;
//    }




}
